package com.zy.openchat.core.model.photo;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.core.model.file.FileInfo;

import java.util.List;

public class PhotoInfo implements Parcelable {
    public List<PhotoSizeInfo> sizes;
    public boolean has_stickers;



    public PhotoSizeInfo messagePhoto() {
        if (sizes != null && sizes.size() > 0) {
            for (PhotoSizeInfo info : sizes) {
                if ("i".equals(info.type)) {
                    if (info.photo != null && info.photo.local != null && info.photo.local.isExist()) {
                        return info;
                    }
                }
            }
            for (PhotoSizeInfo info : sizes) {
                if ("x".equals(info.type)) {
                    if (info.photo != null && info.photo.local != null) {
                        return info;
                    }
                }
            }
            return sizes.get(0);
        }
        return null;
    }

    public PhotoSizeInfo previewPhoto() {
        if (sizes != null && sizes.size() > 0) {
            for (PhotoSizeInfo info : sizes) {
                if ("i".equals(info.type)) {
                    if (info.photo != null && info.photo.local != null && info.photo.local.isExist()) {
                        return info;
                    }
                }
            }
            for (PhotoSizeInfo info : sizes) {
                if ("y".equals(info.type)) {
                    if (info.photo != null && info.photo.local != null) {
                        return info;
                    }
                }
            }
            return sizes.get(sizes.size() - 1);
        }
        return null;
    }

    public Boolean isPhotoDownloaded() {
        PhotoSizeInfo size = messagePhoto();
        if (size != null) {
            return size.isPhotoDownloaded();
        }
        return false;
    }

    public long totalSize() {
        PhotoSizeInfo size = messagePhoto();
        if (size != null&&size.photo!=null) {
            return size.photo.expected_size;
        }
        return 0;
    }

    public long uploadedSize() {
        PhotoSizeInfo size = messagePhoto();
        if (size != null&&size.photo!=null&&size.photo.remote!=null) {
            return size.photo.remote.uploaded_size;
        }
        return 0;
    }

    public String getPhotoImagePath(){
        PhotoSizeInfo size = messagePhoto();
        if (size != null && size.photo != null) {
             return size.photo.local.path;
        }
        return null;
    }

    public long getPhotoFileId() {
        PhotoSizeInfo size = messagePhoto();
        if (size != null && size.photo != null) {
            return size.photo.id;
        }
        return 0;
    }

    public String getRemotePhotoFileId() {
        PhotoSizeInfo size = messagePhoto();
        if (size != null && size.photo != null&&size.photo.remote!=null) {
            return size.photo.remote.id;
        }
        return "";
    }

    public boolean updatePhotoFile(FileInfo file) {
        if(getPhotoFileId() == file.id){
            messagePhoto().photo = file;
            return true;
        }
        return false;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.sizes);
        dest.writeByte(this.has_stickers ? (byte) 1 : (byte) 0);
    }

    public void readFromParcel(Parcel source) {
        this.sizes = source.createTypedArrayList(PhotoSizeInfo.CREATOR);
        this.has_stickers = source.readByte() != 0;
    }

    public PhotoInfo() {
    }

    protected PhotoInfo(Parcel in) {
        this.sizes = in.createTypedArrayList(PhotoSizeInfo.CREATOR);
        this.has_stickers = in.readByte() != 0;
    }

    public static final Creator<PhotoInfo> CREATOR = new Creator<PhotoInfo>() {
        @Override
        public PhotoInfo createFromParcel(Parcel source) {
            return new PhotoInfo(source);
        }

        @Override
        public PhotoInfo[] newArray(int size) {
            return new PhotoInfo[size];
        }
    };
}
